package member.model.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import member.domain.Member;

import common.util.DataBaseManager;

public class MemberDAOImpl implements MemberDAO {
	
	private DataBaseManager dbManager;
	public MemberDAOImpl(DataBaseManager dbManager){
		this.dbManager = dbManager;
	}

	@Override
	public int insertMember(Member member) throws SQLException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "insert into member ( id, password, name, email, join_date) values (?,?,?,?,?)";
		try{
			conn = dbManager.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, member.getId());
			pstmt.setString(2, member.getPassword());
			pstmt.setString(3, member.getName());
			pstmt.setString(4, member.getEmail());
			Date joinDate = new Date(member.getJoinDate().getTime());//java.sql.Date로 변환해 insert
			pstmt.setDate(5, joinDate);
			return pstmt.executeUpdate();
		}finally{
			dbManager.close(conn, pstmt);
		}
	}
	//회원 정보 수정 처리
	@Override
	public int updateMember(Member member) throws SQLException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		//id와 가입일은 변경 안되도록 처리
		String sql = "update member set password=?, name=?, email=? where id=?";
		try{
			conn = dbManager.getConnection();
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, member.getPassword());
			pstmt.setString(2, member.getName());
			pstmt.setString(3, member.getEmail());
			pstmt.setString(4, member.getId());
			return pstmt.executeUpdate();
		}finally{
			dbManager.close(conn, pstmt);
		}
	}


	@Override
	public int deleteMemberById(String id) throws SQLException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "delete from member where id=?";
		try{
			conn = dbManager.getConnection();
			pstmt = conn.prepareStatement(sql);			
			pstmt.setString(1, id);
			return pstmt.executeUpdate();
		}finally{
			dbManager.close(conn, pstmt);
		}
	}
	
	@Override
	public Member selectMemberById(String id) throws SQLException{
		Member member = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		String sql = "select id, password, name, email, join_date from member where id=?";
		try{
			conn = dbManager.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			rset = pstmt.executeQuery();
			if(rset.next()){
				member = new Member(		rset.getString(1),
													 	rset.getString(2),
													 	rset.getString(3),
													 	rset.getString(4),
													 	rset.getDate(5));
			}
		}finally{
			dbManager.close(conn, pstmt, rset);
		}
		return member;
	}

	@Override
	public List<Member> selectMemberList() throws SQLException{
		ArrayList<Member> list = new ArrayList<Member>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		String sql = "select id, password, name, email, join_date from member";
		try{
			conn = dbManager.getConnection();
			pstmt = conn.prepareStatement(sql);
			rset = pstmt.executeQuery();
			while(rset.next()){
				list.add(new Member(	rset.getString(1),
											 	rset.getString(2),
											 	rset.getString(3),
											 	rset.getString(4),
											 	rset.getDate(5)));
			}
		}finally{
			dbManager.close(conn, pstmt, rset);
		}
		return list;
	}
	
	@Override
	public List<Member> selectMemberByName(String name) throws SQLException{
		ArrayList<Member> list = new ArrayList<Member>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		String sql = "select id, password, name, email, join_date from member where name like '%'||?||'%'";
		try{
			conn = dbManager.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			rset = pstmt.executeQuery();
			while(rset.next()){
				list.add(new Member(	rset.getString(1),
											 	rset.getString(2),
											 	rset.getString(3),
											 	rset.getString(4),
											 	rset.getDate(5)));
			}
		}finally{
			dbManager.close(conn, pstmt, rset);
		}
		return list;
	}
}











